package com.linjacy.httpproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.linjacy.httpproject.annotation.NeedToAuth;
import com.linjacy.httpproject.annotation.NeedToLog;
import com.linjacy.httpproject.component.LogQueueTaskHandler;
import com.linjacy.httpproject.component.QueueGenerationService;
import com.linjacy.httpproject.entity.User;
import com.linjacy.httpproject.exception.SystemErrorException;
import com.linjacy.httpproject.request.RegisterUserRequest;
import com.linjacy.httpproject.response.R;
import com.linjacy.httpproject.service.ItemService;
import com.linjacy.httpproject.service.TestService;
import com.linjacy.httpproject.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    TestService testService;

    @Autowired
    UserService userService;

    @Autowired
    ItemService itemService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("hello")
    public String hello(){
        return "Hello World";
    }

    @RequestMapping("testRest")
    public void TestRest(){
        testService.testRest();
    }

    @Autowired
    QueueGenerationService queueGenerationService;

    @Autowired
    LogQueueTaskHandler logQueueTaskHandler;

    //@NeedToAuth
    @NeedToLog
    //@NeedToEncrypt
    @RequestMapping("getUserById")
    //@DS("db2")
    public R getUserById(@RequestParam String id ,@RequestHeader String token) throws SystemErrorException {
        System.out.println(token);
        User userById = userService.getUserById(id);
        //queueGenerationService.addTask(logQueueTaskHandler);
        return R.success().put("data", userById);
    }

    @NeedToAuth
    @RequestMapping("getUserByRule")
    public R getUserByRule(){
        List<User> userList = userService.getUserByRule();
        return R.success().put("data", userList);
    }

    @PostMapping("json2Sql")
    public R json2Sql(@RequestBody JSONObject json) throws SystemErrorException {
        return R.success().put("data", userService.json2Sql(json));
    }

    /**
     *  注册
     */
    //@NeedToDecrypt
    @RequestMapping("register")
    public R registerUser(@RequestBody @Valid RegisterUserRequest user){
        userService.registerUser(user);
        return R.success("注册成功");
    }

    /**
     * 登录
     */
    @RequestMapping("login")
    public R loginUser(@RequestBody @Valid RegisterUserRequest user, HttpServletRequest request) throws Exception{
        String token = userService.loginUser(user);
        return R.success("登录成功").put("token", token);
    }

    /**
     * 获取用户列表
     */
    @RequestMapping("getUserList")
    public R getUserList(){
        return R.success("登录成功").put("data", userService.getUserList());
    }

    /**
     * 获取项目统计列表数据
     */
    @RequestMapping("getItemSta")
    public R getItemSta(@RequestParam String date) throws Exception{
        return R.success("计算成功").put("data", itemService.getItemSta(date));
    }

    /**
     * 导出
     * @param
     * @param response
     */
    @GetMapping(value = "/exportExcel")
    public void exportConfig(HttpServletResponse response) {
    //ByteArrayOutputStream out = userService.export(request, baseConfTableType, dbTableType);
        userService.getExportList(response);
    }

    /**
     * 导入
     */
    @PostMapping("importUserList")
    public R importUserList(@RequestBody MultipartFile file){
        System.out.println(file);
        userService.importUserList(file);
        return R.success("导入成功");
    }


    /**
     * 测试分组
     */
    @GetMapping(value = "/testGroup")
    public R testGroup(){
        return R.success("查询成功").put("data", itemService.testGroup());
    }


    /**
     * 测试mq推送消息
     */
    @GetMapping(value = "/sendMessage")
    public R sendMessage(){
        Map<String, Object> data = new HashMap<>();
        data.put("Date", new Date());
        data.put("Msg", "Test RabbitMq Message");
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestBiding", data);
        return R.success("发送成功");
    }



}
